#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2016 lizongzhe 
#
# Distributed under terms of the MIT license.

import re
import logging
import requests
import base64
from bs4 import BeautifulSoup
from proxy_manager.core import ProxyHost
from proxy_manager.resource.base import BaseProxyResource

logger = logging.getLogger(__name__)

SCHEMES = (
    ('https', 'https'),
    ('http', 'http'),
)

AVAILABILITY = [(value, ">={}%".format(value)) for value in range(0, 100, 10)]

class ProxyResource(BaseProxyResource):
    #url = 'http://dev.kuaidaili.com/api/getproxy/?orderid=917209582572120&num=100&area=%E5%A4%A7%E9%99%86&b_pcchrome=1&b_pcie=1&b_pcff=1&b_android=1&b_iphone=1&b_ipad=1&protocol=2&method=1&an_tr=1&an_an=1&an_ha=1&sp1=1&sp2=1&quality=1&sort=1&format=json&sep=1'
    url = 'http://dev.kuaidaili.com/api/getproxy/?orderid=917209582572120&num=1000&area=%E4%B8%AD%E5%9B%BD&protocol=2&method=1&an_tr=1&an_an=1&an_ha=1&format=json&sep=1&quality=1'
    def __init__(self, *args, **kwargs):
        pass

    def get_proxies(self):
        try:
            resp = requests.get(self.url)
            data = resp.json()
            proxies = data['data']['proxy_list']
            return [ProxyHost(ip=p.split(':')[0], port=p.split(':')[1], scheme='https') for p in proxies ]
        except Exception as e:
            logger.error(e, exc_info=True)
            raise e

